#!/bin/bash

# APK Resign Tool - Main Entry Script
# 这是APK重签名工具的主入口脚本，负责解析命令行参数并调用相应的功能模块

# 获取脚本所在目录的绝对路径
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# 加载各个功能模块
source "$SCRIPT_DIR/modules/config.sh"
source "$SCRIPT_DIR/modules/utils.sh"
source "$SCRIPT_DIR/modules/dependencies.sh"
source "$SCRIPT_DIR/modules/unpack.sh"
source "$SCRIPT_DIR/modules/build.sh"
source "$SCRIPT_DIR/modules/sign.sh"
source "$SCRIPT_DIR/modules/keystore.sh"
source "$SCRIPT_DIR/modules/launch.sh"

# 显示使用帮助信息
show_usage() {
    local build_tools_path=$(get_build_tools_path)
    
    echo "APK 重签名工具"
    echo "用法:"
    echo "  $0 check                                  - 检查依赖"
    echo "  $0 unpack <apk文件> [输出目录]               - 解包APK文件"
    echo "  $0 build <输入目录> [输出APK]                - 从目录构建APK"
    echo "  $0 sign <未签名APK> <密钥库> [别名]           - 使用apksigner签名APK"
    echo "  $0 build-sign <输入目录> <密钥库> [别名] [输出APK] - 构建并签名APK"
    echo "  $0 resign <APK文件> <密钥库> [别名]           - 解包、构建并签名APK"
    echo "  $0 genkey <密钥库文件> [别名]                 - 生成新的密钥库"
    echo "  $0 set-path <build-tools路径>               - 设置Android build-tools路径"
    echo "  $0 launch <APK文件>                         - 分析APK包名并启动应用"
    echo "  $0 help                                    - 显示此帮助信息"
    echo ""
    
    if [ -n "$build_tools_path" ]; then
        echo "当前build-tools路径: $build_tools_path"
    else
        echo "build-tools路径未设置。使用 '$0 set-path' 进行设置。"
    fi
}

# 主函数
main() {
    if [ $# -lt 1 ]; then
        show_usage
        exit 1
    fi
    
    local command="$1"
    shift
    
    case "$command" in
        check)
            check_dependencies
            ;;
        unpack)
            if [ $# -lt 1 ]; then
                echo "错误: 缺少APK文件"
                show_usage
                exit 1
            fi
            unpack_apk "$@"
            ;;
        build)
            if [ $# -lt 1 ]; then
                echo "错误: 缺少输入目录"
                show_usage
                exit 1
            fi
            build_apk "$@"
            ;;
        sign)
            if [ $# -lt 2 ]; then
                echo "错误: 缺少参数"
                show_usage
                exit 1
            fi
            sign_apk "$@"
            ;;
        build-sign)
            if [ $# -lt 2 ]; then
                echo "错误: 缺少参数"
                show_usage
                exit 1
            fi
            local input_dir="$1"
            local keystore_file="$2"
            local alias_name="${3:-my-key-alias}"
            local output_apk="$4"
            local storepass="${5:-android}"
            local keypass="${6:-android}"
            
            local unsigned_apk
            if [ -z "$output_apk" ]; then
                unsigned_apk="$(mktemp).unsigned.apk"
            else
                unsigned_apk="$(mktemp).unsigned.apk"
            fi
            
            build_apk "$input_dir" "$unsigned_apk" && \
            sign_apk "$unsigned_apk" "$keystore_file" "$alias_name" "$output_apk" "$storepass" "$keypass"
            
            local result=$?
            rm -f "$unsigned_apk"
            exit $result
            ;;
        resign)
            if [ $# -lt 2 ]; then
                echo "错误: 缺少参数"
                show_usage
                exit 1
            fi
            resign_apk "$@"
            ;;
        genkey)
            if [ $# -lt 1 ]; then
                echo "错误: 缺少密钥库文件"
                show_usage
                exit 1
            fi
            generate_keystore "$@"
            ;;
        set-path)
            if [ $# -lt 1 ]; then
                echo "错误: 缺少参数"
                show_usage
                exit 1
            fi
            set_build_tools_path "$@"
            ;;
        launch)
            if [ $# -lt 1 ]; then
                echo "错误: 缺少APK文件"
                show_usage
                exit 1
            fi
            analyze_and_launch "$@"
            ;;
        help|--help|-h)
            show_usage
            ;;
        *)
            echo "错误: 未知命令: $command"
            show_usage
            exit 1
            ;;
    esac
}

# 运行主函数
main "$@" 